package pl.softech.engine3d.input;

public class GameAction {
	
	public enum Behavior {
		NORMAL,
		DETECT_INITIAL_PRESS_ONLY
	}
	
	public enum State {
		RELEASD,
		PRESSED,
		WAITING_FOR_RELEASE
	}
	
	private String name;
	private Behavior behavior;
	private State state;
	private int amount;
	
	public GameAction(String name) {
		this(name, Behavior.DETECT_INITIAL_PRESS_ONLY);
	}
	
	public GameAction(String name, Behavior behavior) {
		this.behavior = behavior;
		this.name = name;
		reset();
	}
	
	public void reset() {
		amount = 0;
		state = State.RELEASD;
	}
	
	public String getName() { return name; }
	
	public synchronized int getAmount() {
		int ret = amount;
		if(ret != 0) {
			if(state == State.RELEASD)
				amount = 0;
			else if(behavior == Behavior.DETECT_INITIAL_PRESS_ONLY) {
				state = State.WAITING_FOR_RELEASE;
				amount = 0;
			}
		}
		return ret;
	}
	
	public synchronized void press(int amount) {
		if(state == State.WAITING_FOR_RELEASE) return;
		this.amount += amount;
		state = State.PRESSED;
	}
	
	public synchronized boolean isPressed() { return (getAmount() != 0); }

	public synchronized void release() { state = State.RELEASD; }
	
	public synchronized void press() { press(1); }
	
	public synchronized void tap() {
		press();
		release();
	}
	
	@Override
	public boolean equals(Object obj) {
		
		if(this == obj) return true;
		if(obj instanceof GameAction) {
			GameAction ga = (GameAction) obj;
			if(ga.name.equals(name) && ga.behavior.equals(behavior))
				return true;
		}
		
		return false;
	}
}
